require(["config"],function(){
	//引入依赖模块
	require(["jquery","header"],function($){
		function Register(){
			this.getValidataCode();
			this.addListener();
		}
		
		$.extend(Register.prototype, {
			
			addListener: function() {
				
				$(".code-random").on("click", $.proxy(this.getValidataCode, this));
				$("#check-code").on("blur", this.checkValidataCode);
				$("#register-btn").on("click" , $.proxy(this.registerHandler, this));
				$("#username").on("blur" , this.checkuserName);
				$("#password").on("blur" ,this.checkPassword);
				$("#check-password").on("blur" ,this.recheckPassword);
			},
			
			
			//验证用户名
			checkuserName : function(){
				const _username = $("#username")[0].value;
				if(!_username){
					$(".username-notes")[0].innerText = "用户名不能为空";
					return;
				}else{
					const reg = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
					if(reg.test(_username)){
						$(".username-notes")[0].innerText = "用户名格式正确";
						
					}else{
						$(".username-notes")[0].innerText = "用户名格式不正确";
					}
				}
			},
			//验证密码
			checkPassword : function(){
				const _password = $("#password")[0].value;
				if(!_password){
					$(".password-notes")[0].innerText = "密码不能为空（6-20位包含数字，字母，字符）";
				}else{
					const reg1= /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*].{6,20}$/;
					const reg2 =/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*].{6,20}$/;
					const reg3 = /^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+).{6,20}$/;
					if(reg1.test(_password)){
						$(".password-notes")[0].innerText = "密码强度：强";
					}else if(reg2.test(_password)){
						$(".password-notes")[0].innerText = "密码强度：中";
					}else if(reg3.test(_password)){
						$(".password-notes")[0].innerText = "密码强度：弱";
					}else{
						$(".password-notes")[0].innerText = "密码格式不正确（6-20位包含数字，字母，字符）";
					}
				}
			},
			//二次密码验证
			recheckPassword : function(){
				const _password = $("#password")[0].value,
					  _repsd = $("#check-password")[0].value;
				if(_password !== _repsd){
					$(".psdck-notes")[0].innerText = "二次输入密码不正确，请从新输入";
				}else{
					$(".psdck-notes")[0].innerText = "密码确认成功";
				}
			},
			//获取验证码函数
			getValidataCode:function(){
				$.ajax({
					url : "http://route.showapi.com/932-2?showapi_appid=29550&showapi_sign=fe01daee5a6a400c91e9f8523813fc61&length=6",
					dataType : "json",
					success : function(data) {
						// 显示出验证码图片
						$("#validate_code")[0].src = data.showapi_res_body.image;
						console.log(data.showapi_res_body.image);
						// 保存验证码关联标识：将关联标识缓存到DOM元素对象中，将在校验有效性时使用
						$("#validate_code")[0].sid = data.showapi_res_body.sid;
						//console.log(data.showapi_res_body.image+"图片");
					}
				});
				$("#check-code")[0].value = "";
				$(".code-notes")[0].innerText = "";
			},
			//验证验证码是否正确
			checkValidataCode:function(){
				var _checkcode = this.value,
					_sid = $("#validate_code")[0].sid,
					_url = `http://route.showapi.com/932-1?showapi_appid=29550&showapi_sign=fe01daee5a6a400c91e9f8523813fc61&checkcode=${_checkcode}&sid=${_sid}`;
				console.log(_checkcode,_sid);
				if(!_checkcode ){
					$(".code-notes")[0].innerText = "验证码不能为空";
					return;
				}
				
				$.ajax({
					url : _url,
					dataType : "json",
					success : function(data) {
						if (data.showapi_res_body.valid) {
							$(".code-notes")[0].innerText = "正确";
							
						} else {
							$(".code-notes")[0].innerText = "验证码错误";
							console.log(data.showapi_res_body.valid);
						}
					}
				});
			},
			
			//注册
			registerHandler: function(){
				
				 const _username = $("#username")[0].value,
				 	   _password = $("#password")[0].value,
				 	   _repsd = $("#check-password")[0].value,
				 	   _checkcode = $("#check-code")[0].value;
				 if(!_username && !_password && !_repsd && !_checkcode)	{
				 	alert("	请填写信息");
				 }else{
				 	if(!_username){
						$(".username-notes")[0].innerText = "用户名不能为空";
						alert("用户名不能为空");
					}
					if(!_password){
						$(".password-notes")[0].innerText = "密码不能为空";
						alert("密码不能为空");
					}
					if(!_repsd){
						$(".psdck-notes")[0].innerText = "确认密码不能为空";
						alert("确认密码不能为空");
					}
					if(!_checkcode){
						$(".code-notes")[0].innerText = "验证码不能为空";
						alert("验证码不能为空");
					}
				 }
				
				var data = $(".form-input").serialize();
				var url ="http://localhost:80/bastcake-pc-project/api/register.php";
				
				$.post(url, data, this.regSuccessHandler, "json");
				return false;
			},
			
			regSuccessHandler:function(data){
				if (data.res_code === 1) { // 注册成功
					location = "/html/login.html";
				} else { // 注册失败
					alert("注册失败：" + data.res_message);
				}
			}
			
		});
		new Register();
	});
});
